package com.csx.map;



/**
 *  基于二分搜索树的Map
 * @param <K> 键
 * @param <V> 值
 */
public class BinarySearchTreeMap<K extends Comparable<K>, V> implements Map<K,V> {

    private class Node{
        /* 键*/
        public K key;

        /** 值  */
        public V value;

        /** 左右树  */
        public Node left, right;

        public Node( K key, V val){
            this.key = key;
            this.value = val;
            this.left = null;
            this.right = null;
        }


        @Override
        public String toString(){
            return key.toString();
        }
    }
    /* 根*/
    private Node root;

    /* 大小*/
    private int size;

    public BinarySearchTreeMap(){
        root = null;
        size = 0;
    }

    private Node getNode(Node node,K key){
        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) == 0){
            return node;
        }else if (key.compareTo(node.key) < 0){
            return getNode(node.left, key);
        }else{
            return getNode(node.right, key);
        }

    }

    @Override
    public void add(K key, V value) {
        root = add(root,key,value);
    }

    /**
     * 向以node为根的二分搜索树中插入元素e, 递归算法
     * @param node 根
     * @param key 键
     * @param value 值
     * @return 新的根
     */
    private Node add(Node node, K key, V value) {
        if (node == null){
            size ++;
            return new Node(key,value);
        }
        if (key.compareTo(node.key) < 0){
            node.left = add(node.left,key,value);
        }else if (key.compareTo(node.key) > 0){
            node.right = add(node.right,key,value);
        }else{
            node.value = value;
        }
        return node;
    }

    @Override
    public V remove(K key)  {
        Node node = getNode(root, key);
        if (node != null){
            root = remove(root,key);
            return node.value;
        }
        return null;
    }

    @Override
    public boolean contains(K key) {
        return getNode(root,key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        return node == null ? null : node.value;
    }

    @Override
    public void set(K key, V value) throws IllegalAccessException {
        Node node = getNode(root, key);
        if(node == null){
            throw new IllegalAccessException(key + " 不存在");
        }
        node.value = value;
    }

    @Override
    public int gitSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 删除掉以node为根的二分搜索树中值为e的节点, 递归算法
     * @param root 根
     * @param key 键
     * @return 删除以后的值
     */
    private Node remove(Node root, K key) {
        if (root == null){
            return null;
        }
        if (key.compareTo(root.key) < 0){
            root.left = remove(root.left,key);
            return root;
        }else if (key.compareTo(root.key) > 0){
            root.right = remove(root.right,key);
            return root;
        }else{
            if (root.right == null){
                Node leftNode = root.left;
                root.left = null;
                size --;
                return leftNode;
            }
            if (root.left == null){
                Node rightNode = root.right;
                root.right = null;
                size --;
                return rightNode;
            }
            /*
             *待删除节点左右子树均不为空的情况
             * 找到比待删除节点大的最小节点,  即待删除节点右子树的最小节点
             * 用这个节点顶替待删除节点的位置
             * */
            Node minimum = minimum(root.right);
            minimum.right = removeMin(root.right);
            minimum.left = root.left;
            root.left = root.right = null;
            return minimum;
        }
    }
    /**
     * 返回以root 为根的二分搜索树的最小值所在的节点
     * @param root 根
     * @return 最小值所在的节点
     */
    private Node minimum(Node root) {
        if (root.left == null){
            return root;
        }
        return minimum(root.left);
    }

    /**
     * 删除最小值所在的节点
     * @return 最小值
     */
    public Node removeMin(){
        Node minimum = minimum(root);
        root = removeMin(root);
        return minimum;
    }

    /**
     * 删除掉以node为根的二分搜索树中最小的节点
     * @param root 根
     * @return 返回新的根
     */
    private Node removeMin(Node root) {
        if (root.left == null){
            Node rightNode = root.right;
            root.right = null;
            size --;
            return rightNode;
        }
        root.left = removeMin(root.left);
        return root;
    }

    public static void main(String[] args) throws IllegalAccessException {
        BinarySearchTreeMap<Integer,String> linkedListMap = new BinarySearchTreeMap<>();
        for (int i = 0; i < 5; i++) {
            linkedListMap.add(i,"user" + i);
        }
        linkedListMap.set(3,"csx");
        linkedListMap.remove(2);
        linkedListMap.get(3);
    }
}
